package com.flask.colorpicker.cameralibrary;

import com.flask.colorpicker.cameralibrary.constant.Constants;
import com.flask.colorpicker.cameralibrary.listener.CaptureListener;
import com.flask.colorpicker.cameralibrary.util.ValueAnimator;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;

import static com.flask.colorpicker.cameralibrary.JCameraView.BUTTON_STATE_ONLY_CAPTURE;

public class CaptureButton extends Component implements Component.DrawTask, Component.TouchEventListener{
    public static final int STATE_IDLE = 0x001; // 空闲状态
    public static final int STATE_PRESS = 0x002; // 按下状态
    public static final int STATE_BAN = 0x005; // 禁止状态

    private int mPressState; // 当前按钮状态
    private final int mButton_state; // 按钮可执行的功能状态（拍照,录制,两者）

    private final Paint mPaint;

    // 中心坐标
    private final float mCenter_X;
    private final float mCenter_Y;

    private final float mButton_outside_radius; // 外圆半径
    private float mButton_inside_radius; // 内圆半径

    private CaptureListener mCaptureListener; // 按钮回调接口

    public CaptureButton(Context context, int size) {
        super(context);
        // 按钮半径
        float mButton_radius = size / 2.0f;

        mButton_outside_radius = mButton_radius;
        mButton_inside_radius = mButton_radius * 0.75f;

        // 进度条宽度
        // 长按外圆半径变大的Size
        int mOutside_add_size = size / 5;
        // 长安内圆缩小的Size

        mPaint = new Paint();
        mPaint.setAntiAlias(true);

        mPressState = STATE_IDLE; // 初始化为空闲状态
        mButton_state = BUTTON_STATE_ONLY_CAPTURE; // 初始化按钮为拍照

        mCenter_X = (size + mOutside_add_size * 2) / 2f;
        mCenter_Y = (size + mOutside_add_size * 2) / 2f;

        addDrawTask(this);
        setTouchEventListener(this);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        mPaint.setStyle(Paint.Style.FILL_STYLE);

        // 外圆背景色
        int mOutside_color = 0xEEDCDCDC;
        mPaint.setColor(new Color(mOutside_color)); // 外圆（半透明灰色）
        canvas.drawCircle(mCenter_X, mCenter_Y, mButton_outside_radius, mPaint);

        // 内圆背景色
        int mInside_color = 0xFFFFFFFF;
        mPaint.setColor(new Color(mInside_color)); // 内圆（白色）
        canvas.drawCircle(mCenter_X, mCenter_Y, mButton_inside_radius, mPaint);
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent event) {
        switch (event.getAction()) {
            case TouchEvent.PRIMARY_POINT_DOWN:
                if (event.getPointerCount() > 1 || mPressState != STATE_IDLE) {
                    if (mCaptureListener != null) {
                        mCaptureListener.onError(
                                "click button By state is STATE_BAN " + "or TouchEvent getPointerCount > 1",
                                Constants.ERROR_TYPE_PICTURE);
                    }
                    break;
                }

                // Touch_Event_Down时候记录的Y值
                event.getPointerPosition(event.getIndex()).getY();
                mPressState = STATE_PRESS; // 修改当前状态为点击按下

                break;
            case TouchEvent.POINT_MOVE:
                break;
            case TouchEvent.PRIMARY_POINT_UP:
                // 根据当前按钮的状态进行相应的处理
                handlerUnPressByState();
                break;
        }
        return true;
    }
    // 当手指松开按钮时候处理的逻辑
    private void handlerUnPressByState() {
        // 根据当前状态处理
        // 当前是点击按下
        if (mPressState == STATE_PRESS) {
            if (mCaptureListener != null
                    && (mButton_state == BUTTON_STATE_ONLY_CAPTURE)) {
                startCaptureAnimation(mButton_inside_radius);
            } else {
                mPressState = STATE_IDLE;
            }
        }
    }

    // 内圆动画
    private void startCaptureAnimation(float insideStart) {
        ValueAnimator insideAnimation = ValueAnimator.ofFloat(insideStart, insideStart * 0.75f, insideStart);
        insideAnimation.addUpdateListener(
                (animation, value) -> {
                    mButton_inside_radius = (float) value;
                    invalidate();
                });
        insideAnimation.addListener(
                new ValueAnimator.AnimatorStatusChangeListener() {
                    @Override
                    public void onEnd(ValueAnimator animator, ValueAnimator.State state) {
                        if (state == ValueAnimator.State.ANIM_END) {
                            mCaptureListener.takePictures();
                            mPressState = STATE_BAN;
                        }
                    }

                    @Override
                    public void start(ValueAnimator animator, ValueAnimator.State state) {
                    }
                });
        insideAnimation.setDuration(100);
        insideAnimation.start();
    }
    /**
     * 设置回调接口
     *
     * @param captureListener CaptureListener
     */
    public void setCaptureListener(CaptureListener captureListener) {
        this.mCaptureListener = captureListener;
    }

    /**
     * 设置状态
     */
    public void resetState() {
        mPressState = STATE_IDLE;
    }
}
